package edu.nf.bs.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.bs.dao.OrderInfoMapper;
import edu.nf.bs.dao.impl.OrderInfoMapperImpl;
import edu.nf.bs.entity.DeliveryMan;
import edu.nf.bs.entity.OrderInfo;
import edu.nf.bs.exception.AddException;
import edu.nf.bs.exception.UpdateOrderException;
import edu.nf.bs.service.OrderInfoService;
import edu.nf.bs.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrderInfoServiceImpl implements OrderInfoService {
    private OrderInfoMapper orderInfoMapper;

    public void setOrderInfoMapper(OrderInfoMapper orderInfoMapper) {
        this.orderInfoMapper = orderInfoMapper;
    }

    @Override
    public Double getPrice() {
        Double price = orderInfoMapper.getPrice();
        if(price==null){
            return 0.0;
        }
        return price;
    }

    @Override
    public Double getPrices() {
        Double mysqlDate = orderInfoMapper.getMysqlDate();
        if(mysqlDate==null){
            return 0.0;
        }
        return mysqlDate;
    }

    @Override
    public int getOrderCount() {
        Integer monthOrder = orderInfoMapper.getMonthOrder();

        if(monthOrder==null){
            return 0;
        }
        return monthOrder;
    }

    @Override
    public int getOrderCounts() {
        Integer monthOrder = orderInfoMapper.getMonthOrders();
        System.out.println("-----------------"+monthOrder);
        if(monthOrder==null){
            return 0;
        }
        return monthOrder;
    }
    @Override
    public PageInfo<OrderInfo> listOrder(String orderId, Integer pageNum, Integer pageSize) {
        List<OrderInfo> list = orderInfoMapper.listOrder(orderId, pageNum, pageSize);

        PageInfo<OrderInfo> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public Double[] getYearPrice() {
        Double[] yearPrice = orderInfoMapper.getYearPrice();
        return yearPrice;
    }

    @Override
    public void addOrderInfo(OrderInfo orderInfo) {
        Integer integer = orderInfoMapper.addOrderInfo(orderInfo);
        if(integer==0){
            throw new AddException("订单添加失败");
        }
    }

    @Override
    public void updateOrderInfo(String orderId, float orderPrice) {
        Integer integer = orderInfoMapper.updateOrderInfo(orderId, orderPrice);
        if(integer==0){
            throw new UpdateOrderException("订单修改失败");
        }
    }

    @Override
    public void updateLogistics(String expressId, String logisticsId) {
        Integer integer = orderInfoMapper.updateLogistics(expressId, logisticsId);
        if(integer!=1){
            throw new UpdateOrderException("订单修改失败");
        }
    }

    @Override
    public void updateOrderStatus(String logisticsId) {
        Integer integer = orderInfoMapper.updateOrderStatus(logisticsId);
        if(integer!=1){
            throw new UpdateOrderException("修改订单状态失败");
        }
    }

    @Override
    public String queryLogistics(String expressId) {
        OrderInfo orderInfo = orderInfoMapper.queryLogistics(expressId);
        if(orderInfo!=null){
            return orderInfo.getLogisticsId();
        }
        return null;
    }

    @Override
    public DeliveryMan getOrderMax() {
        DeliveryMan orderMax = orderInfoMapper.getOrderMax();
        if(orderMax!=null){
            return orderMax;
        }
        return null;
    }

    @Override
    public PageInfo<OrderInfo> queryUserOrder(String userId,Integer pageNum, Integer pageSize) {
        List<OrderInfo> orderInfos = orderInfoMapper.queryOrderInfo(userId, pageNum, pageSize);
        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfos);
        return orderInfoPageInfo;
    }
}
